﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProjectEulerSolutions
{
    /*
     * The cube, 41063625 (345^3), can be permuted to produce two other cubes: 56623104 (384^3) and 66430125 (405^3). In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube.

Find the smallest cube for which exactly five permutations of its digits are cube.

     * 
     * */
    class Problem62 : IProblem
    {
        public string Calculate()
        {
            //taktika
            //Napravit dictionary<long, List<long>>, dje cemo spremat cubeove izpocetka
            //ako naidjemo na neki ciji value ima 5 zapisa, damo najmanji od tih
            //key je orderby od znamenki
            //69ms -.-

            Dictionary<long, List<long>> dictionary = new Dictionary<long, List<long>>();

            int limit = 10000;

            int target = 5;

            for (long i = 10; i < limit; i++)
            {
                long cube = i * i * i;
                var digits = CommonFunctions.GetDigits(cube).OrderBy(x => x).ToList(); //descending da ih kasnije obrne
                long key = digits.Select(x=>(long)x).Select((x, ind) => x * (long)Math.Pow(10, ind)).Sum();
                if (dictionary.ContainsKey(key))
                {
                    dictionary[key].Add(cube);
                }
                else
                {
                    dictionary.Add(key, new List<long>() { cube });
                }
            }

            foreach (var list in dictionary.Values)
            {
                if (list.Count == target)
                {
                    return list.Min().ToString();
                }
            }

            return " ";
        }
    }
}
